package com.cn.kehong.handlerInterceptor;

import com.cn.kehong.utils.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Interceptor implements HandlerInterceptor {


    @Autowired
    private RedisTemplate redisTemplate;


    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String requestUri = httpServletRequest.getRequestURI();
        String contextPath = httpServletRequest.getContextPath();
        String url = requestUri.substring(contextPath.length());
        System.out.println("Interceptor:"+url);
        if(Constants.WHITELIST.contains(url)||url.indexOf(Constants.API_URL) >-1
                ||url.indexOf(Constants.DOWNLOAD_URL) >-1||url.indexOf(Constants.DIST) >-1){
            return true;
        }
        // 用户没有登陆跳转到登陆页面
//        String id = httpServletRequest.getSession().getId();
//        Object object =  redisTemplate.opsForValue().get(id);
//        if(null == object){
//            httpServletResponse.sendRedirect(contextPath+"/");
//            return false;
//        }

        return true;
    }

    //在请求处理方法执行之后执行
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

    }

    //在dispatcherServlet处理后执行,做清理工作.
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
